package com.itmg.analyze.jp.data.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.itmg.analyze.jp.bean.AnalyzeBean;
import com.itmg.analyze.jp.data.IDataAnalyze;
import com.itmg.vo.SearchResultItemVO;
import com.itmg.vo.SearchResultVO;
import com.itmg.vo.TargetDataVO;

/**
 *  dom解析yahoo
 * @author xuehaipeng
 * @version 1.0
 * */

public class YahooDataAnlyzeImpl implements IDataAnalyze {
	
	protected Logger log = Logger.getLogger(YahooDataAnlyzeImpl.class.getName());
	
	public YahooDataAnlyzeImpl() {
	}
	
	public SearchResultVO getSearchResultItemVO(TargetDataVO targetDataVO) throws Exception {
		    Document theDocument = AnalyzeBean.parseDocument(targetDataVO.getUrl());
		    String sourceSiteName = targetDataVO.getSourceSiteName();
		    SearchResultVO searchResultVO = new SearchResultVO();
		    if(theDocument == null)
		    	return searchResultVO;
			Element theRootElement = theDocument.getDocumentElement();
			List<SearchResultItemVO> searchResultItemVOList = new ArrayList<SearchResultItemVO>();
			boolean isHaveNextdata = false;
			if(theRootElement.getAttributes().getNamedItem("totalResultsAvailable") != null)
			{
				String totalNumber = theRootElement.getAttributes().getNamedItem("totalResultsAvailable").getNodeValue();
				if(totalNumber != null && !"".equals(totalNumber.trim()) && !"0".equals(totalNumber.trim()))
				{
					searchResultVO.setTotalResultNumber(Integer.valueOf(totalNumber));
					isHaveNextdata = true;
				}
			}
			NodeList theFirstNodeList = theRootElement.getElementsByTagName("Hit");
			for(int i=0;i < theFirstNodeList.getLength();i++)
			{
				Node machingNode = theFirstNodeList.item(i);
				if(machingNode.getNodeName().equals("Hit") && isHaveNextdata == true)
				{
					SearchResultItemVO searchResultItemVO = new SearchResultItemVO();
					NodeList nodeCrawlerList = machingNode.getChildNodes();
					for(int j=0;j<nodeCrawlerList.getLength();j++)
					{
						Node itemNode = nodeCrawlerList.item(j);
						if(itemNode.getNodeName().equals("Name"))
						{
							String productName = itemNode.getTextContent().trim();
							if(!"".equals(productName))
							{
								searchResultItemVO.setProductName(productName);
							}			
						}
						if(itemNode.getNodeName().equals("Description"))
						{
							String Comment = itemNode.getTextContent().trim();
							if(!"".equals(Comment))
							{
								searchResultItemVO.setComment(Comment);
							}					
						}
						if(itemNode.getNodeName().equals("Url"))
						{
							String productUrl = itemNode.getTextContent().trim();
							if(!"".equals(productUrl))
							{
								searchResultItemVO.setProductUrl(productUrl);
							}				
						}
						if(itemNode.getNodeName().equals("ProductId"))
						{
							String productID = itemNode.getTextContent().trim();
							if(!"".equals(productID))
							{
								searchResultItemVO.setProductID(productID);
							}			
						}
						if(itemNode.getNodeName().equals("Price"))
						{
							String lowestPrice = itemNode.getTextContent().trim();
							if(!"".equals(lowestPrice))
							{
								searchResultItemVO.setLowestPrice(Integer.parseInt(lowestPrice));
							}			
						}
						if(itemNode.getNodeName().equals("Image"))
						{
							NodeList imageList = itemNode.getChildNodes();
							for(int a=0;a<imageList.getLength();a++)
							{
								Node imageNode = imageList.item(a);
								if(imageNode.getNodeName().equals("Small"))
								{
									String smallImageUrl = imageNode.getTextContent().trim();
									if(!"".equals(smallImageUrl))
									{
										searchResultItemVO.setSmallImageUrl(smallImageUrl);
									}						
								}
								if(imageNode.getNodeName().equals("Medium"))
								{
									String bigImageUrl = imageNode.getTextContent().trim();
									if(!"".equals(bigImageUrl))
									{
										searchResultItemVO.setBigImageUrl(bigImageUrl);
									}						
								}
							}
						}
					}
					String md5 = searchResultItemVO.getMyProductMd5();
					searchResultItemVO.setProductMd5(md5);
					searchResultItemVO.setSourceSiteName(sourceSiteName);
					searchResultItemVOList.add(searchResultItemVO);
				}
			}
			searchResultVO.setResultItemList(searchResultItemVOList);
			return searchResultVO; 
	}
}
